package com.kaizige.realtor.Controller;

import com.kaizige.realtor.Dao.messageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class msgController {
	@Autowired
	messageDao md;
	@GetMapping("/ownMessage")
	public String toOwnMessage(HttpSession session, Model model){
		String user = (String)session.getAttribute("LoginUser");
		List<Map<String, Object>> toMes = md.getToMes(user);
		List<Map<String, Object>> fromMes = md.getFromMes(user);
		model.addAttribute("toMes", toMes);
		model.addAttribute("fromMes",fromMes);
		return "/msg/ownMessages";
	}
	
	@RequestMapping("/ownSetMsgSta/{id}")
	public String ownSetMsgSta(@PathVariable("id") int id){
		md.setMsgSta(id);
		return "redirect:/ownMessage";
	}
	
	@GetMapping("/manMessage")
	public String toManMessage(HttpSession session, Model model){
		String user = (String)session.getAttribute("LoginUser");
		List<Map<String, Object>> toMes = md.getToMes(user);
		List<Map<String, Object>> fromMes = md.getFromMes(user);
		model.addAttribute("toMes", toMes);
		model.addAttribute("fromMes",fromMes);
		return "/msg/manMessages";
	}
	
	@RequestMapping("/manSetMsgSta/{id}")
	public String manSetMsgSta(@PathVariable("id") int id){
		md.setMsgSta(id);
		return "redirect:/manMessage";
	}
	
	@GetMapping("/tenMessage")
	public String toTenMessage(HttpSession session, Model model){
		String user = (String)session.getAttribute("LoginUser");
		List<Map<String, Object>> toMes = md.getToMes(user);
		List<Map<String, Object>> fromMes = md.getFromMes(user);
		model.addAttribute("toMes", toMes);
		model.addAttribute("fromMes",fromMes);
		return "/msg/tenMessages";
	}
	
	@RequestMapping("/tenSetMsgSta/{id}")
	public String tenSetMsgSta(@PathVariable("id") int id){
		md.setMsgSta(id);
		return "redirect:/tenMessage";
	}
}
